import 'package:changxindianzishangwu/db/ec_cache.dart';
import 'package:changxindianzishangwu/pages/find/report_page.dart';
import 'package:changxindianzishangwu/pages/home_page.dart';
import 'package:changxindianzishangwu/provider/currentMessageList.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_operation_result.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_value_callback.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';

class FriendSettings extends StatefulWidget {
  String userId;
  FriendSettings({Key key, this.userId}) : super(key: key);

  @override
  _FriendSettingsState createState() => _FriendSettingsState();
}

class _FriendSettingsState extends State<FriendSettings> {
  var _lights = false;

  @override
  void initState() {
    super.initState();
    print("===1111===" + EcCache.getInstance().get("userId"));
  }

  deleteFriends() async {
    V2TimValueCallback<List<V2TimFriendOperationResult>> res =
        await TencentImSDKPlugin.v2TIMManager
            .getFriendshipManager()
            .deleteFromFriendList(
      userIDList: [widget.userId],
      deleteType: 2, //双向好友
    );
    if (res.code == 0) {
      // 删除成功
      print("======" + widget.userId);
      TencentImSDKPlugin.v2TIMManager.v2ConversationManager
          .deleteConversation(conversationID: "c2c_" + widget.userId)
          .then((value) {
        {
          if (value.code == 0) {
            showToast("删除成功");
            Provider.of<CurrentMessageListModel>(context, listen: false)
                .deleteMessage("c2c_" + widget.userId);
          }
          setState(() {});
        }
      });

      print("======" + EcCache.getInstance().get("userId"));
      showToast('删除成功');
      Get.offAll(() => HomePage());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(
          "好友设置",
          style: TextStyle(fontSize: ScreenUtil().setSp(16)),
        ),
        centerTitle: true,
      ),
      body: Column(
        children: [
          Container(
            alignment: Alignment.center,
            color: Colors.white,
            margin: EdgeInsets.only(top: 10),
            child: Column(
              children: <Widget>[
                // MergeSemantics(
                //   child: ListTile(
                //     title: Text(
                //       '加入黑名单',
                //       style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                //     ),
                //     trailing: CupertinoSwitch(
                //       activeColor: Color.fromRGBO(0, 220, 169, 1),
                //       value: _lights,
                //       onChanged: (bool value) async {
                //         setState(() {
                //           _lights = !_lights;
                //         });
                //
                //         print("comming!");
                //         if (_lights) {
                //           print("comming!");
                //           V2TimValueCallback<List<V2TimFriendOperationResult>>
                //               res = await TencentImSDKPlugin.v2TIMManager
                //                   .getFriendshipManager()
                //                   .addToBlackList(userIDList: [widget.userId]);
                //           if (res.code == 0) {
                //             List<V2TimFriendOperationResult> opres = res.data;
                //             print("黑名单返回${opres[0].resultCode}");
                //             if (opres[0].resultCode == 0) {
                //               showToast("操作成功");
                //             } else {
                //               showToast("操作失败");
                //             }
                //           }
                //         } else {
                //           V2TimValueCallback<List<V2TimFriendOperationResult>>
                //               res = await TencentImSDKPlugin.v2TIMManager
                //                   .getFriendshipManager()
                //                   .deleteFromBlackList(
                //                       userIDList: [widget.userId]);
                //           if (res.code == 0) {
                //             List<V2TimFriendOperationResult> opres = res.data;
                //             print("黑名单返回${opres[0].resultCode}");
                //             if (opres[0].resultCode == 0) {
                //               showToast("操作成功");
                //             } else {
                //               showToast("操作失败");
                //             }
                //           }
                //         }
                //       },
                //     ),
                //     onTap: () async {},
                //   ),
                // ),
                Divider(
                  height: 1,
                  indent: ScreenUtil().setWidth(10),
                  color: Color.fromRGBO(248, 248, 248, 1),
                  thickness: ScreenUtil().setHeight(1),
                ),
                InkWell(
                  onTap: () {
                    Get.to(ReportPage(1, widget.userId));
                  },
                  child: ListTile(
                    leading: Text(
                      "投诉/举报",
                      style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                    ),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
              ],
            ),
          ),
          Container(
            alignment: Alignment.center,
            color: Colors.white,
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(10)),
            child: InkWell(
              onTap: () {
                // setAppLoginToken("");
                // onKickedOffline();
                // Get.offAllNamed("/loginInit");

                deleteFriends();
              },
              child: ListTile(
                title: Container(
                  alignment: Alignment.center,
                  height: ScreenUtil().setHeight(50),
                  width: ScreenUtil().setWidth(50),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Text(
                    "删除",
                    style: TextStyle(
                        color: Colors.red, fontSize: ScreenUtil().setSp(14)),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
